﻿using System.Collections.Generic;
using System.Windows;

namespace XAMLStylePad.Models
{
    public class ResourceDictionaryModel
    {
        ResourceDictionary _resources;
        Dictionary<object, object> _keysByValues = new Dictionary<object,object>();

        public ResourceDictionaryModel(ResourceDictionary resources)
        {
            _resources = resources;
            foreach(var key in resources.Keys)
            {
                object res;
                try
                { // for example, objects like Strings cannot be constructed, because of parameterless constructor is epsend
                    res = resources[key];
                    if(res != null && !(res is string))
                        _keysByValues.Add(res, key);
                }
                catch
                {
                    continue;
                }
                
                if(res is Style)
                    _Styles.Add(res as Style);
                else if(res is FrameworkTemplate)
                    _Templates.Add(res as FrameworkTemplate);
            }
        }

        public object GetValuesKey(object value)
        {
            if(_keysByValues.ContainsKey(value)){
                var key = _keysByValues[value];
                return key;
            }
            return null;
        }

        public string FileName
        {
            get;
            set;
        }

        #region Styles
        public List<Style> Styles
        {
            get
            {
                return _Styles;
            }
        }
        List<Style> _Styles = new List<Style>();
        #endregion

        #region Templates
        public List<FrameworkTemplate> Templates
        {
            get
            {
                return _Templates;
            }
        }
        List<FrameworkTemplate> _Templates = new List<FrameworkTemplate>();
        #endregion
    }
}
